<?php	namespace Admin\Model;
use Hdphp\Model\Model;
class Goods extends Model
{
	protected $table = 'goods';
	protected $validate = array(
		array('category_cid','require','请选择所属分类',2,3),
		array('brand_bid','require','请选择所属品牌',2,3),
		array('gname','require','请填写商品名称',2,3),
		array('marketprice','require','请填写市场价',2,3),
		array('shopprice','require','请填写商城价',2,3),
		array('gclick','require','请填写点击次数',2,3),
		array('intro','require','请填写商品详情',2,3),
		array('service','require','填写售后服务信息',2,3),
	);
	protected $auto = array(
		array('gtime','time','function',3,3),
	);
	/**
	 * 首页
	 */
	public function getAll()
	{	//分页数据
		$page = Page::row(20)->make($this->count());
		//所有数据
		$data = $this->orderBy('gid','DESC')->get();
		//将分页数据和所有数据返出
		return array('page'=>$page,'data'=>$data);
	}
	/**
	 * 添加
	 */
	public function store()
	{	//执行自动验证
		if(!$this->create())   return false;
		//将后台管理员id压入$this->data一起添加到商品表
		$this->data['admin_aid'] = $_SESSION['aid'];
		//执行商品表添加
		$gid=$this->add();
		//2.商品详情表添加
		$pic=Q('post.pic');//列表图
		$photo=Q('post.photo');//商品图册
		$small='';$medium='';$big='';
		//商品图册处理。每一张图执行三次缩略成不同尺寸，将大图放一起
		foreach ($photo as $k => $v) {
			$small .= Image::thumb($v, dirname($v) . '/small' . basename($v), 60, 60, 5) . ',';
			$medium .= Image::thumb($v, dirname($v) . '/medium' . basename($v), 482, 482, 5) . ',';
			$big .= Image::thumb($v, dirname($v) . '/big' . basename($v), 964, 964, 5) . ',';
		}
		//将最右变得，去掉
		$small = rtrim($small,',');$medium=rtrim($medium,',');$big=rtrim($big,',');
		//组合要执行添加的数组数据
		$goodsdetail = array(
			'small' 	=> $small,
			'medium'	=> $medium,
			'big'		=> $big,
			'intro'		=> $_POST['intro'],
			'service'	=> $_POST['service'],
			'goods_gid' => $gid
		);
		$goodsDetailModel = new \Admin\Model\GoodsDetails;//实例化商品详情表
		$goodsAttrModel = new \Admin\Model\GoodsAttr;//实例化商品属性表
		//添加商品详情表
		$goodsDetailModel->add($goodsdetail);
		//3.商品属性表添加
		//如果有选择属性
		if(!empty($_POST['attr']))
		{	//循环进行添加
			foreach ($_POST['attr'] as $k => $v) {
				//只处理被选择过的属性，没有选择的不做处理
				if($v!='0')
				{	//组合要上传的数组数据
					$data = array('gavalue'=>$v,'typeAttr_taid'=>$k,'goods_gid'=>$gid,);
				}
				//商品属性表添加
				$goodsAttrModel->add($data);
			}
		}
		//如果有选择规格
		if(!empty($_POST['spec']))
		{	//循环进行添加
			foreach ($_POST['spec'] as $k => $v) {
				//循环进行添加
				for($i=0;$i<count($v['tavalue']);$i++)
				{	//所要添加的数据数据
					$data = array(
						'gavalue'=> $v['tavalue'][$i],
						'added'  =>$v['added'][$i],
						'typeAttr_taid'=>$k,
						'goods_gid'=>$gid,						
					);
					//执行商品属性表添加
					$goodsAttrModel->add($data);
				}
				
			}
		}
		return true;
	}
	/**
	 * 编辑
	 */
	public function edit()
	{
		$gid=Q('post.gid',0,'intaval');
		if(!$this->create()) return false;
		$this->data['admin_aid'] = $_SESSION['aid'];
		$this->save();
		//商品属性表
		$goodsDetailModel = new \Admin\Model\GoodsDetails;
		$goodsAttrModel = new \Admin\Model\GoodsAttr;
		$goodsAttrModel->where('goods_gid',$gid)->delete(); 
		if(!empty($_POST['attr']))
		{
			foreach ($_POST['attr'] as $k => $v) {
				if($v!='0')
				{
					$data = array(
						'gavalue'=>$v,
						'typeAttr_taid'=>$k,
						'goods_gid'=>$gid,
					);
				}
				$goodsAttrModel->add($data);
			}
		}	
		if(!empty($_POST['spec']))
		{
			foreach ($_POST['spec'] as $k => $v) {
				for($i=0;$i<count($v['tavalue']);$i++)
				{
					$data = array(
						'gavalue'=> $v['tavalue'][$i],
						'added'  =>$v['added'][$i],
						'typeAttr_taid'=>$k,
						'goods_gid'=>$gid,						
					);
					$goodsAttrModel->add($data);
				}
				
			}
		}
		//商品详情表
		$pic=Q('post.pic');
		$photo=Q('post.photo');
		$small='';$medium='';$big='';
		foreach ($photo as $k => $v) {
			$small .= Image::thumb($v, dirname($v) . '/small' . basename($v), 60, 60, 5) . ',';
			$medium .= Image::thumb($v, dirname($v) . '/medium' . basename($v), 482, 482, 5) . ',';
			$big .= Image::thumb($v, dirname($v) . '/big' . basename($v), 964, 964, 5) . ',';
		}
		$small = rtrim($small,',');$medium=rtrim($medium,',');$big=rtrim($big,',');
		$goodsdetail = array(
			'small' 	=> $small,
			'medium'	=> $medium,
			'big'		=> $big,
			'intro'		=> $_POST['intro'],
			'service'	=> $_POST['service'],
			'goods_gid' => $gid
		);
		$goodsDetailModel->where('goods_id',$gid)->update($goodsdetail);			
		return true;
	}
	/**
	 * 递归找子集
	 */
	public function getSon($data,$cid)
	{
		static $temp = array();
		foreach ($data as $k => $v) {
			if($v['pid']==$cid)
			{
				$temp[] = $v['cid'];
				$this->getSon($data,$v['cid']);
			}
		}
		return $temp;
	}
}
